package _2022.hot100._226_翻转二叉树;

/**
 * @author： YHSimon
 * @date： 2022-05-16 15:48
 */
class TreeNode{
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(){}
    TreeNode(int val){this.val=val;}
    TreeNode(int val,TreeNode left,TreeNode right){this.val=val;this.left=left;this.right=right;}
}
public class Solution {
    // 先交换再递归
    public TreeNode invertTree(TreeNode root){
        if(root!=null){
            TreeNode tmp=root.left;
            root.left=root.right;
            root.right=tmp;
            invertTree(root.left);
            invertTree(root.right);
        }
        return root;
    }

    // 先递归再交换
    public TreeNode invertTree2(TreeNode root){
        if(root!=null){
            TreeNode left=invertTree2(root.left);
            TreeNode right=invertTree2(root.right);
            root.left=right;
            root.right=left;
        }
        return root;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
    }
}
